#!/bin/bash  mysql 集群
echo -e "拉取mysql5.7.41镜像"
docker pull mysql:5.7.41

echo -e "创建自定义网络"
docker network create --subnet=172.18.0.0/18 mynet

if ! docker container inspect mysql_1 &>/dev/null; then

#	echo -e "创建容器数据目录 /root/mysql_1/data"
#	mkdir -p /root/mysql_1/data

#	echo -e "创建容器数据目录 /root/mysql_1/config"
#	mkdir -p /root/mysql_1/config

	echo -e "启动 MySQL_1 容器，并挂载数据目录和配置目录"
	docker run -it -d --name mysql_1 -p 7001:3306 --net mynet --ip 172.18.0.2 -m 400m -v /root/mysql_1/data:/var/lib/mysql -v /root/mysql_1/config:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=abc123456 -e TZ=Asia/Shanghai mysql:5.7.41 --lower_case_table_names=1
	echo -e "等待容器启动"
    while ! docker exec mysql_1 mysqladmin ping --silent &> /dev/null; do
    sleep 20
    done

	echo -e "进入容器并执行 MySQL 创建用户命令"
    docker exec -it mysql_1 bash -c 'mysql -uroot -pabc123456 -e "GRANT RELOAD, REPLICATION SLAVE, SUPER ON *.* TO '\''sync'\''@'\''%'\'' IDENTIFIED BY '\''abc123456'\''; GRANT SELECT, INSERT, UPDATE, DELETE ON *.* TO '\''sync'\''@'\''%'\''; FLUSH PRIVILEGES;"'
    sleep 5
	echo -e "停止 mysql_1 容器"
	docker stop mysql_1
	sleep 10

    echo -e "配置my.cnf 主从环境"
	echo -e "[mysqld]\ncharacter_set_server = utf8\nserver_id = 1\nlog_bin = mysql_bin\nrelay_log = relay_bin\nlog-slave-updates = 1\nsql_mode = NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES" > /root/mysql_1/config/my.cnf

	echo -e "重启 MySQL_1容器"
	docker start mysql_1
fi

if ! docker container inspect mysql_2 &>/dev/null; then

#	echo -e "创建容器数据目录 /root/mysql_2/data"
#	mkdir -p /root/mysql_2/data

#	echo -e "创建容器数据目录 /root/mysql_2/config"
#	mkdir -p /root/mysql_2/config

	echo -e "启动 MySQL_2 容器，并挂载数据目录和配置目录"
    docker run -it -d --name mysql_2 -p 7002:3306 --net mynet --ip 172.18.0.3 -m 400m -v /root/mysql_2/data:/var/lib/mysql -v /root/mysql_2/config:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=abc123456 -e TZ=Asia/Shanghai --privileged=true mysql:5.7.41 --lower_case_table_names=1

	echo -e "等待容器启动"
	sleep 10

	echo -e "关闭 mysql_2 容器"
	docker stop mysql_2


	echo -e "配置my.cnf 主从环境"
	echo -e "[mysqld]\ncharacter_set_server = utf8\nserver_id = 2\nlog_bin = mysql_bin\nrelay_log = relay_bin\nread-only = 1\nsql_mode = NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES" > /root/mysql_2/config/my.cnf

	echo -e "重启 mysql_2 "
	docker start mysql_2
	sleep 10

    echo "设置订阅MySQL_1的binlog"
    docker exec -it mysql_2 bash -c "mysql -uroot -pabc123456 -e 'stop slave;change master to master_host=\"172.18.0.2\", master_port=3306, master_user=\"sync\", master_password=\"abc123456\";start slave; select sleep (3); show slave status;'"
fi


if ! docker container inspect mysql_3 &>/dev/null; then

	echo -e "启动 MySQL_3 容器，并挂载数据目录和配置目录"
    docker run -it -d --name mysql_3 -p 7003:3306 --net mynet --ip 172.18.0.4 -m 400m -v /root/mysql_3/data:/var/lib/mysql -v /root/mysql_3/config:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=abc123456 -e TZ=Asia/Shanghai --privileged=true mysql:5.7.41 --lower_case_table_names=1

	echo -e "等待容器启动"
	sleep 10

	echo -e "关闭 mysql_3 容器"
	docker stop mysql_3


	echo -e "配置my.cnf 主从环境"
	echo -e "[mysqld]\ncharacter_set_server = utf8\nserver_id = 3\nlog_bin = mysql_bin\nrelay_log = relay_bin\nread-only = 1\nsql_mode = NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES" > /root/mysql_3/config/my.cnf

	echo -e "重启 mysql_3 "
	docker start mysql_3
	sleep 10

    echo "设置订阅MySQL_1的binlog"
    docker exec -it mysql_3 bash -c "mysql -uroot -pabc123456 -e 'stop slave;change master to master_host=\"172.18.0.2\", master_port=3306, master_user=\"sync\", master_password=\"abc123456\";start slave; select sleep (3); show slave status;'"
fi


if ! docker container inspect mysql_4 &>/dev/null; then

	echo -e "启动 MySQL_1 容器，并挂载数据目录和配置目录"
    docker run -it -d --name mysql_4 -p 7004:3306 --net mynet --ip 172.18.0.5 -m 400m -v /root/mysql_4/data:/var/lib/mysql -v /root/mysql_4/config:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=abc123456 -e TZ=Asia/Shanghai --privileged=true mysql:5.7.41 --lower_case_table_names=1

	echo -e "等待容器启动"
    sleep 20

	echo -e "进入容器并执行 MySQL 创建用户命令"
    docker exec -it mysql_4 bash -c 'mysql -uroot -pabc123456 -e "GRANT RELOAD, REPLICATION SLAVE, SUPER ON *.* TO '\''sync'\''@'\''%'\'' IDENTIFIED BY '\''abc123456'\''; GRANT SELECT, INSERT, UPDATE, DELETE ON *.* TO '\''sync'\''@'\''%'\''; FLUSH PRIVILEGES;"'

	echo -e "停止mysql_1容器"
	docker stop mysql_4
	sleep 10

    echo -e "配置my.cnf 主从环境"
	echo -e "[mysqld]\ncharacter_set_server = utf8\nserver_id = 4\nlog_bin = mysql_bin\nrelay_log = relay_bin\nlog-slave-updates = 1\nsql_mode = NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES" > /root/mysql_4/config/my.cnf

	echo -e "重启 MySQL_1容器"
	docker start mysql_4
fi

if ! docker container inspect mysql_5 &>/dev/null; then

	echo -e "启动 MySQL_5 容器，并挂载数据目录和配置目录"
    docker run -it -d --name mysql_5 -p 7005:3306 --net mynet --ip 172.18.0.6 -m 400m -v /root/mysql_5/data:/var/lib/mysql -v /root/mysql_5/config:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=abc123456 -e TZ=Asia/Shanghai --privileged=true mysql:5.7.41 --lower_case_table_names=1

	echo -e "等待容器启动"
	sleep 10

	echo -e "关闭 mysql_5 容器"
	docker stop mysql_5


	echo -e "配置my.cnf 主从环境"
	echo -e "[mysqld]\ncharacter_set_server = utf8\nserver_id = 5\nlog_bin = mysql_bin\nrelay_log = relay_bin\nread-only = 1\nsql_mode = NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES" > /root/mysql_5/config/my.cnf

	echo -e "重启 mysql_5 "
	docker start mysql_5
	sleep 10

    echo "设置订阅 MySQL_5 的binlog"
    docker exec -it mysql_5 bash -c "mysql -uroot -pabc123456 -e 'stop slave;change master to master_host=\"172.18.0.5\", master_port=3306, master_user=\"sync\", master_password=\"abc123456\";start slave; select sleep (3); show slave status;'"
fi


if ! docker container inspect mysql_6 &>/dev/null; then

	echo -e "启动 MySQL_6 容器，并挂载数据目录和配置目录"
    docker run -it -d --name mysql_6 -p 7006:3306 --net mynet --ip 172.18.0.7 -m 400m -v /root/mysql_6/data:/var/lib/mysql -v /root/mysql_6/config:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=abc123456 -e TZ=Asia/Shanghai --privileged=true mysql:5.7.41 --lower_case_table_names=1

	echo -e "等待容器启动"
	sleep 10

	echo -e "关闭 mysql_6 容器"

	docker stop mysql_6


	echo -e "配置my.cnf 主从环境"
	echo -e "[mysqld]\ncharacter_set_server = utf8\nserver_id = 6\nlog_bin = mysql_bin\nrelay_log = relay_bin\nread-only = 1\nsql_mode = NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES" > /root/mysql_6/config/my.cnf

	echo -e "重启 mysql_6 "
	docker start mysql_6
	sleep 10

    echo "设置订阅 MySQL_6 的binlog"
    docker exec -it mysql_6 bash -c "mysql -uroot -pabc123456 -e 'stop slave;change master to master_host=\"172.18.0.5\", master_port=3306, master_user=\"sync\", master_password=\"abc123456\";start slave; select sleep (3); show slave status;'"
fi



    docker exec -it mysql_1 bash -c "mysql -uroot -pabc123456 -e 'stop slave;change master to master_host=\"172.18.0.5\", master_port=3306, master_user=\"sync\", master_password=\"abc123456\";start slave; select sleep (3); show slave status;'"

    docker exec -it mysql_4 bash -c "mysql -uroot -pabc123456 -e 'stop slave;change master to master_host=\"172.18.0.2\", master_port=3306, master_user=\"sync\", master_password=\"abc123456\";start slave; select sleep (3); show slave status;'"
